<template>
  <div class="channel-edit">
    <van-cell center :border="false">
      <div slot="title" class="channel-title">我的频道</div>
      <!-- 默认插槽 放在右边 -->
      <van-button
        @click="isEdit = !isEdit"
        class="editBtn"
        type="danger"
        plain
        round
        size="mini"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>

    <van-grid :gutter="14">
      <van-grid-item
        class="grid-item"
        :class="{ active: index === currentIndex }"
        :icon="isEdit && index !== 0 ? 'clear' : ''"
        v-for="(channel, index) in userChannels"
        :key="index"
        :text="channel.name"
        @click="onUserChannelClick(channel, index)"
      />
    </van-grid>

    <van-cell center :border="false">
      <div slot="title" class="channel-title">频道推荐</div>
    </van-cell>

    <van-grid :gutter="14">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        @click="onAdd(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannels,
  deleteUserChannels,
} from "@/api/channel";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage";

export default {
  name: "channel-edit",
  props: {
    userChannels: {
      type: Array,
      required: true,
    },
    // 激活的频道
    currentIndex: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      // 所有频道数据列表
      allChannels: [],
      // 控制编辑的显示状态
      isEdit: false,
    };
  },
  computed: {
    ...mapState(["user"]),
    // 推荐的频道列表
    recommendChannels() {
      // 思路：所以频道 - 我的频道 = 剩下的推荐频道
      // filter 方法：过滤数据，根据方法返回布尔值为 true 来收集数据
      return this.allChannels.filter((item) => {
        // 判断 item 是否属于用户频道
        return !this.userChannels.find((useritem) => {
          return useritem.id === item.id;
        });
      });
    },
  },
  created() {
    this.loadAllChannels();
  },
  methods: {
    // 加载频道数据
    async loadAllChannels() {
      const { data } = await getAllChannels();
      this.allChannels = data.data.channels;
    },
    // 点击频道添加
    async onAdd(channel) {
      this.userChannels.push(channel);

      // TODO: 数据持久化
      if (this.user) {
        // 登录了，数据存到线上
        await addUserChannels({
          channels: [
            // seq：顺序序号 就是有多少个频道数据
            { id: channel.id, seq: this.userChannels.length },
          ],
        });
      } else {
        // 没有登录，数据存储到本地
        setItem("user-channels", this.userChannels);
      }
    },
    // 点击频道：切换到频道 / 删除频道
    onUserChannelClick(channel, index) {
      if (this.isEdit && index !== 0) {
        // 编辑状态 删除
        this.deleteChannel(channel, index);
      } else {
        this.switchChannel(index);
      }
    },
    // 删除频道
    async deleteChannel(channel, index) {
      // 如果删除的是当前激活频道之前的频道
      if (index <= this.currentIndex) {
        // 更新激活频道的索引
        this.$emit("update-active", this.currentIndex - 1);
      }
      this.userChannels.splice(index, 1);

      // 数据持久化
      if (this.user) {
        await deleteUserChannels(channel.id);
      } else {
        // 没有登录，数据存储到本地
        setItem("user-channels", this.userChannels);
      }
    },
    // 切换到频道
    switchChannel(index) {
      // 关闭弹出层
      this.$emit("close");

      // 切换频道
      this.$emit("update-active", index);
    },
  },
};
</script>
<style lang='less' scoped>
.channel-edit {
  padding-top: 108px;
  .editBtn {
    padding: 10px 25px;
    font-size: 23px;
  }
  .channel-title {
    font-size: 32px;
    color: #333;
    font-weight: 600;
  }
  .grid-item {
    width: 160px;
    height: 86px;
    /deep/ .van-grid-item__content {
      background: #f4f4f8;
      .van-grid-item__text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
    }
    /deep/ .van-grid-item__icon {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 42px;
      color: #aaaaaa;
    }
  }
  /deep/ .active {
    .van-grid-item__content {
      background: #ece4e4;
    }
    .van-grid-item__text {
      font-weight: 600;
      color: #f85a5a !important;
    }
  }
}
</style>